package com.lt.bookkeeping.Fragment;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.lt.bookkeeping.R;
import com.lt.bookkeeping.Utils.StringUtils;
import com.lt.bookkeeping.Utils.ToastUtils;
import com.lt.bookkeeping.Utils.Utils;
import com.lt.bookkeeping.coustomView.FlowLayout;
import com.lt.bookkeeping.coustomView.FlowTag;
import com.lt.bookkeeping.http.RequestHttp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * @author LiuTong
 */
public class FragmentPersonInfo extends BaseFragment{
    private final String TAG = getClass().getSimpleName();
    private ImageView headImageView;
    private TextView tvName,btn_sync;
    private LinearLayout rootLayout;
    private EditText ipEdit;
    private Handler handler = new Handler(Looper.getMainLooper());
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_personal,container,false);
        initView(view);
        initEvent();
        return view;
    }

    private void initView(View view) {

        rootLayout = view.findViewById(R.id.root_layout);

        btn_sync = view.findViewById(R.id.personal_sync);
        headImageView = view.findViewById(R.id.personal_head_image);
        tvName = view.findViewById(R.id.personal_name);
        ipEdit = view.findViewById(R.id.personal_ip);
        Utils.setTextView(btn_sync,getString(R.string.icon_sync));
        Glide.with(this)
                .load(R.drawable.girl)
                .placeholder(R.drawable.head5)
                .fallback(R.drawable.head6)
                .circleCrop()
                .into(headImageView);

        Utils.setTextView(tvName, getString(R.string.icon_main));
        FlowLayout flowLayout = new FlowLayout(getContext());
        String res = getString(R.string.data_select);

        for (int i = 0; i < 6; i++) {
            FlowTag tv = new FlowTag(getContext());
            tv.setText(StringUtils.getRandomString(res));
            flowLayout.addView(tv);
        }
        flowLayout.setMargin(25, 25, 25, 25);
        rootLayout.addView(flowLayout,2);
        flowLayout.setOnTagCancelClickListen(view1 -> flowLayout.removeView(view1));
    }

    private void initEvent() {

        headImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        btn_sync.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("Recycle")
            @Override
            public void onClick(View v) {

                TranslateAnimation translateAnimation = new TranslateAnimation(
                        0f,-100f,0f,0f);
                translateAnimation.setDuration(700);
                translateAnimation.setFillEnabled(true);
//                btn_sync.startAnimation(translateAnimation);
                RotateAnimation animator = new RotateAnimation(
                        0f,360f,
                        Animation.RELATIVE_TO_SELF,0.5f,
                        Animation.RELATIVE_TO_SELF,0.5f);


                animator.setRepeatMode(Animation.RESTART);
                animator.setDuration(1500);
                animator.setFillAfter(true);
                animator.setRepeatCount(1);
                animator.setInterpolator(new LinearInterpolator());
                btn_sync.startAnimation(animator);

//                AnimationSet animationSet = new AnimationSet(true);
//                animationSet.addAnimation(translateAnimation);
//                animationSet.addAnimation(animator);
//                btn_sync.startAnimation(animationSet);
                String ipString = ipEdit.getText().toString().trim();
                if(StringUtils.isEmptyOrNull(ipString)){
                    ipString = "https://192.168.99.230:12345";
                }
                try {
                    RequestHttp requestHttp = new RequestHttp(ipString);
                    requestHttp.startRequest(new Callback() {
                        @Override
                        public void onFailure(@NonNull Call call, @NonNull IOException e) {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                   Log.e(TAG,"访问失败"+e);
                                }
                            });
                        }

                        @Override
                        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {

                            if(response.isSuccessful()) {
                                Log.e(TAG,"访问成功");
                                StringBuilder sb = new StringBuilder();
                                sb.append("类型 : ")
                                        .append(Objects.requireNonNull(response.body()).contentType() + "\r\n")
                                        .append("长度 : ")
                                        .append(Objects.requireNonNull(response.body()).contentLength() + "\r\n")
                                        .append("内容字符串 : ");
                                BufferedReader bufferedReader = new BufferedReader(
                                        new InputStreamReader(
                                                Objects.requireNonNull(response.body()).byteStream(),
                                                StandardCharsets.UTF_8));
                                String content = null;
                                while ((content = bufferedReader.readLine()) != null) {
                                    sb.append(content).append("\n");
                                }
                                Log.e(TAG, sb.toString());
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
